package com.evan.review.architecture.mvp.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 用户数据仓库
 * MVP架构中的Model层，负责数据的存储和业务逻辑
 * 
 * 在实际应用中，这里应该使用Room、SQLite或网络API来实现数据持久化
 * 为了简化示例，这里使用内存存储
 */
public class UserRepository {
    private static UserRepository instance;
    private final Map<Long, User> users;
    private final AtomicLong idCounter;
    
    private UserRepository() {
        users = new HashMap<>();
        idCounter = new AtomicLong(0);
        
        // 添加示例数据
        addSampleData();
    }
    
    public static synchronized UserRepository getInstance() {
        if (instance == null) {
            instance = new UserRepository();
        }
        return instance;
    }
    
    private void addSampleData() {
        // 添加示例用户
        addUser(new User(
                idCounter.incrementAndGet(),
                "张三",
                "zhangsan@example.com",
                "13800138001",
                28,
                "https://randomuser.me/api/portraits/men/1.jpg",
                true
        ));
        
        addUser(new User(
                idCounter.incrementAndGet(),
                "李四",
                "lisi@example.com",
                "13800138002",
                32,
                "https://randomuser.me/api/portraits/men/2.jpg",
                true
        ));
        
        addUser(new User(
                idCounter.incrementAndGet(),
                "王五",
                "wangwu@example.com",
                "13800138003",
                25,
                "https://randomuser.me/api/portraits/men/3.jpg",
                false
        ));
        
        addUser(new User(
                idCounter.incrementAndGet(),
                "赵六",
                "zhaoliu@example.com",
                "13800138004",
                35,
                "https://randomuser.me/api/portraits/men/4.jpg",
                true
        ));
        
        addUser(new User(
                idCounter.incrementAndGet(),
                "钱七",
                "qianqi@example.com",
                "13800138005",
                29,
                "https://randomuser.me/api/portraits/men/5.jpg",
                false
        ));
    }
    
    /**
     * 获取所有用户
     */
    public List<User> getAllUsers() {
        return new ArrayList<>(users.values());
    }
    
    /**
     * 获取活跃用户
     */
    public List<User> getActiveUsers() {
        List<User> activeUsers = new ArrayList<>();
        for (User user : users.values()) {
            if (user.isActive()) {
                activeUsers.add(user);
            }
        }
        return activeUsers;
    }
    
    /**
     * 根据ID获取用户
     */
    public User getUserById(long id) {
        return users.get(id);
    }
    
    /**
     * 添加用户
     */
    public User addUser(User user) {
        if (user.getId() <= 0) {
            user.setId(idCounter.incrementAndGet());
        }
        users.put(user.getId(), user);
        return user;
    }
    
    /**
     * 更新用户
     */
    public boolean updateUser(User user) {
        if (users.containsKey(user.getId())) {
            users.put(user.getId(), user);
            return true;
        }
        return false;
    }
    
    /**
     * 删除用户
     */
    public boolean deleteUser(long id) {
        return users.remove(id) != null;
    }
    
    /**
     * 切换用户活跃状态
     */
    public boolean toggleUserActiveStatus(long id) {
        User user = users.get(id);
        if (user != null) {
            user.setActive(!user.isActive());
            return true;
        }
        return false;
    }
    
    /**
     * 搜索用户
     */
    public List<User> searchUsers(String query) {
        List<User> result = new ArrayList<>();
        if (query == null || query.isEmpty()) {
            return getAllUsers();
        }
        
        String lowerCaseQuery = query.toLowerCase();
        for (User user : users.values()) {
            if (user.getName().toLowerCase().contains(lowerCaseQuery) ||
                    user.getEmail().toLowerCase().contains(lowerCaseQuery) ||
                    user.getPhone().contains(lowerCaseQuery)) {
                result.add(user);
            }
        }
        return result;
    }
} 